// pages/shopVideo/videoDetail/videoDetail.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        videoId: null,
        videoInfo: null,
        commentList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false,
        commentDetail: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            videoId: options.videoId
        })
        this.getVideoInfo()
        this.getCommentList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            commentList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getCommentList()
    },

    getVideoInfo: function () {
        wx.showLoading({
            title: '加载中'
        })
        const that = this
        let data = {
            video_id: this.data.videoId
        }
        common.post('Video/video_list', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    videoInfo: res[0]
                })
            }
        }).catch(function (e) { })
    },

    getCommentList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let commentList = this.data.commentList
        let data = {
            video_id: this.data.videoId,
            type: 2,
            page: this.data.nowPage,
            limit: 10
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Video/video_comment_list', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    commentList: commentList,
                    isAll: true
                })
            } else {
                commentList = commentList.concat(res)
                that.setData({
                    commentList: commentList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) { })
    },

    goPraise: function () {
        if (this.data.videoInfo.is_approved == 1) {
            return
        }
        const that = this
        let data = {
            type: 1,
            stuats: '1',
            store_id: wx.getStorageSync('storeId'),
            video_id: this.data.videoId
        }
        common.post('Video/add_approved', data).then(function (res) {
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) { }
                })
                return
            }
            that.getVideoInfo()
        }).catch(function (e) { })
    },

    commentDetailInput: function (e) {
        this.setData({
            commentDetail: e.detail.value
        })
    },

    goComment: function () {
        if (this.data.commentDetail == '') {
            wx.showToast({
                title: '请填写评论内容',
                icon: 'none',
                duration: 2000
            })
            return
        }
        wx.showLoading({
            title: '评论中'
        })
        const that = this
        let data = {
            type: 2,
            store_id: wx.getStorageSync('storeId'),
            video_id: this.data.videoId,
            conent: this.data.commentDetail
        }
        common.post('Video/add_comment', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                wx.showModal({
                    title: '提示',
                    content: res.error,
                    showCancel: false,
                    success(res) { }
                })
                return
            }
            wx.showModal({
                title: '提示',
                content: '评论成功',
                showCancel: false,
                success(res) {
                    if (res.confirm) {
                        that.setData({
                            commentDetail: '',
                            commentList: [],
                            nowPage: 0,
                            isLoading: false,
                            isAll: false
                        })
                        that.getVideoInfo()
                        that.getCommentList()
                    }
                }
            })
        }).catch(function (e) { })
    }
})